package com.xsmart.goods.services.wx;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.goods.dao.GoodsCategoryDao;
import com.xsmart.goods.dao.GoodsManageDao;
import com.xsmart.goods.services.GoodsManageService;
import org.apache.commons.collections.MapUtils;
import org.apache.ibatis.session.ResultContext;
import org.bouncycastle.cert.ocsp.Req;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信商品接口服务
 *
 * @author guojd
 * @version V1.0.0
 */
@Service
public class WxGoodsService {
    @Autowired
    GoodsManageDao goodsManageDao;
    @Autowired
    GoodsCategoryDao goodsCategoryDao;

    /**
     * 查询商品信息
     * @param requestBean
     * @return
     */
    public ResponseBean queryGoods(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer goodsId = requestBean.getRequestParamInteger("goods_id");
        Integer userId = requestBean.getRequestParamInteger("user_id");
        if (goodsId == null) {
            responseBean.setResult(ResultConstant.ERROR);
            responseBean.setMessage("商品不存在或已下架");
        } else {
            Map<String, Object> goodsInfo = goodsManageDao.queryGoodsInfo(goodsId,userId);
            if (goodsInfo == null || goodsInfo.size() == 0) {
                responseBean.setResult(ResultConstant.ERROR);
                responseBean.setMessage("商品不存在或已下架");
            } else {
                List<Map<String,Object>> goodsPhotos=goodsManageDao.queryGoodsPhoto(goodsId);
                if(goodsPhotos.size()==0){
                    Map<String,Object> photoItem=new HashMap<>();
                    photoItem.put("uid","-1");
                    photoItem.put("name",MapUtils.getString(goodsInfo,"goods_img"));
                    goodsPhotos.add(photoItem);
                }
                List<Map<String,Object>> goodsAttr = goodsManageDao.queryGoodsAttr(goodsId);
                goodsInfo.put("chan_shu",goodsAttr);
                goodsInfo.put("img_arr",goodsPhotos);
                responseBean.addResultItem("pro", goodsInfo);
                responseBean.setResult(ResultConstant.SUCCESS);
            }
        }
        return responseBean;
    }

    /**
     * 商品列表
     *
     * @param requestBean 请求对象
     *                    cat_id:分类id,
     *                    ptype:ptype,
     *                    brand_id: 品牌id
     * @return 商品详情
     */
    public ResponseBean queryGoodsList(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Long catId = requestBean.getRequestParamLong("cat_id");
        Long brandId = requestBean.getRequestParamLong("brand_id");
        Long sellerId = requestBean.getRequestParamLong("g_sellerid");
        Long shopId = requestBean.getRequestParamLong("shop_id", -1);
        Integer userId = requestBean.getRequestParamInteger("user_id");
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        Map<String, Object> goodsCatInfo = goodsCategoryDao.queryGoodsCategoryInfo(catId);

        Map<String, Object> goodsMap = new HashMap<>();
        goodsMap.put("cat_id", catId);
        goodsMap.put("cat_path_id", ","+catId+",");
        goodsMap.put("brand_id", brandId);
        if ("-1".equals(String.valueOf(sellerId)) && shopId != -1){
            goodsMap.put("sellerId",shopId);
        }else {
            goodsMap.put("sellerId",sellerId);
        }
        goodsMap.put("userId",userId);
        goodsMap.put("wx",'1');
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        page.setRecords(goodsManageDao.queryGoods(page, goodsMap));
        responseBean.addResultPageItem("pro", page);
        responseBean.addResultItem("cat_name", MapUtils.getString(goodsCatInfo, "cat_name", "错误分类"));
        return responseBean;
    }

    /**
     * 获取顾客评价
     * @param requestBean
     * @return
     */
    public ResponseBean queryCustomerAppraise(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer goods_id = requestBean.getRequestParamInteger("goods_id");
        List<Map<String,Object>> appraiseList = goodsManageDao.queryCustomerAppraise(goods_id);
        responseBean.addResultItem("appraiseList",appraiseList);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 新增商品评价
     * @param requestBean
     * @return
     */
    public ResponseBean addGoodsAppraise(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer orderId = requestBean.getRequestParamInteger("order_id");
        Integer goodsId = requestBean.getRequestParamInteger("goods_id");
        Integer userId = requestBean.getRequestParamInteger("user_id");
        String ogaText = requestBean.getRequestParamString("oga_text");
        Integer ogaShipping = requestBean.getRequestParamInteger("oga_shipping");
        Integer ogaService = requestBean.getRequestParamInteger("oga_service");
        Integer ogaGoods = requestBean.getRequestParamInteger("oga_goods");
        Map<String,Object> appraiseMap = new HashMap<>();
        appraiseMap.put("order_id",orderId);
        appraiseMap.put("goods_id",goodsId);
        appraiseMap.put("user_id",userId);
        appraiseMap.put("oga_text",ogaText);
        appraiseMap.put("oga_shipping",ogaShipping);
        appraiseMap.put("oga_service",ogaService);
        appraiseMap.put("oga_goods",ogaGoods);
        int num = goodsManageDao.addGoodsAppraise(appraiseMap);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 获取相似推荐的商品
     * @param requestBean
     * @return
     */
    public ResponseBean queryRecommend(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        int currentPage = requestBean.getRequestParamInteger("currentPage",1);
        int pageSize = requestBean.getRequestParamInteger("pageSize",3);
        Integer goodsId = requestBean.getRequestParamInteger("goods_id");
        Page<Map<String,Object>> page = new Page<>(currentPage,pageSize);
        page.setRecords(goodsManageDao.queryRecommend(page,goodsId));
        responseBean.addResultPageItem("recommendList",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 查询搜索结果
     * @param requestBean
     * @return
     */
    public ResponseBean querySearchList(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Date day =new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer userId  =requestBean.getRequestParamInteger("userId");
        String  searchValue = requestBean.getRequestParamString("searchValue");
        Integer sellerId = requestBean.getRequestParamInteger("g_sellerid");
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        if (!"".equals(searchValue)){
            Map<String,Object> map = new HashMap<>();
            searchValue = searchValue.trim();
            map.put("userId",userId);
            map.put("searchValue",searchValue);
            map.put("sellerId",sellerId);
            map.put("search_time", df.format(day));

            int deleteNum = goodsManageDao.deleteHisMap(map);
            int addNum = goodsManageDao.addSearchHistroy(map);
            Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
            page.setRecords(goodsManageDao.querySearchList(page, map));
            responseBean.addResultPageItem("searchList",page);
            responseBean.setResult(ResultConstant.SUCCESS);
        }else {
            responseBean.setResult(ResultConstant.ERROR);
            responseBean.setMessage("搜索字段为空！");
            return responseBean;
        }

        return responseBean;
    }


    /**
     * 查询搜索记录
     * @param requestBean
     * @return
     */
    public ResponseBean querySearchHis(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer userId = requestBean.getRequestParamInteger("user_id");
        List<String> searchHis = goodsManageDao.querySearchHis(userId);
        responseBean.addResultItem("searchHisList", searchHis);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
}
